/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package czStvDpds.graphics3D.gui;

import czStvDpds.graphics3D.GraphicsIndex;
import czStvDpds.graphics3D.TextureCreator;
import czStvDpds.graphics3D.geometry.Texture;
import czStvDpds.graphics3D.geometry.TextureProperties;
import czStvDpds.input.LWJGLKeyEvent;
import czStvDpds.logging.Logging;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import javax.imageio.ImageIO;
import org.lwjgl.input.Mouse;

/**
 *
 * @author Ondra
 */
public class LButton extends LComponent {

    private String text;
    private int textX, textY;
    private String command;
    private ArrayList<ButtonListener> listeners;
    private Texture basic, clicked, selected;
    private static final int OUTLINE = 7;

    public LButton(String text) {
        super();
        super.model = new GUIObject(Modeler.generateButtonMesh(loc.getWidth(), loc.getHeight()));
        setText(text);
        command = text;
        listeners = new ArrayList();
        createTextures();
        super.model.setTexture(basic);
    }

    private void createTextures() {
//        Texture buttonTexture = new Texture();
        basic = new Texture();
        clicked = new Texture();
        selected = new Texture();
        BufferedImage imgBasic = null;
        BufferedImage imgClicked = null;
        BufferedImage imgSelected = null;
        try {
            imgBasic = ImageIO.read(new File("resources/models/gui-textures/button-default.png"));
            imgClicked = ImageIO.read(new File("resources/models/gui-textures/button-clicked.png"));
            imgSelected = ImageIO.read(new File("resources/models/gui-textures/button-selected.png"));
        } catch (IOException ex) {
            Logging.getMainLogger().log(Level.WARNING, "Failed to load texture: GUI.LButton, because of {0}", ex.getLocalizedMessage());
        }
        TextureProperties prop = new TextureProperties();
        prop.setType(TextureProperties.TEXTURE_2D);
        prop.setWidth(2);
        prop.setHeight(1);
        prop.setFilterMax(TextureProperties.FILTER_NEAREST);
        basic.setProperties(prop);
        basic.setData(TextureCreator.getImageData(imgBasic));
        clicked.setProperties(prop);
        clicked.setData(TextureCreator.getImageData(imgClicked));
        selected.setProperties(prop);
        selected.setData(TextureCreator.getImageData(imgSelected));

        GraphicsIndex.getGLSynchronizer().generateTexture(basic);
        GraphicsIndex.getGLSynchronizer().generateTexture(clicked);
        GraphicsIndex.getGLSynchronizer().generateTexture(selected);
    }

    /**
     * prida tlacitku posluchac udalosti. Posluchac udalosti dostava do sve
     * metody buttonClicked() informace o klikani na buttonek
     *
     * @param newListener novy posluchac
     */
    public void addButtonListener(ButtonListener newListener) {
        listeners.add(newListener);
    }

    /**
     * odebere dany posluchac ze seznamu posluchacu
     *
     * @param listener posluchac, ktery ma byt odebran
     */
    public void removeButtonListener(ButtonListener listener) {
        listeners.remove(listener);
    }

    public String getCommand() {
        return command;
    }

    /**
     * nastavi specificky string pro tento buttonek, ktery bude mozno dostat z
     * vyvolanych udalosti (viz javax.swing.JButton.setActionCommand())
     *
     * @param command command
     */
    public void setCommand(String command) {
        this.command = command;
    }

    /**
     * zde se definuje vykreslovani buttonku. Predpokladam ze muzu sahat na GL11
     * az GL42, tj. ze Display je vytvoren.
     */
    @Override
    protected void model() {
        this.model.setPosition(loc.getX() + Modeler.pixelsToRatio(offset), loc.getY() + Modeler.pixelsToRatio(offset));
//        this.model.setText(text);
        this.model.setFont(font);
        //stanovovani pozice textu
        textX = ((Modeler.ratioToPixels(loc.getWidth()) - font.getWidth(text)) / 2);
        textY = ((Modeler.ratioToPixels(loc.getHeight()) - font.getHeight(text)) / 2);
//        this.model.setTextX(textX);
//        this.model.setTextY(textY);
        this.model.setText(TextPiece.cut(text, textX, textY, font));
        this.model.resize(Modeler.resizeButtonMesh(loc.getWidth() - (Modeler.pixelsToRatio(offset) * 2), loc.getHeight() - (Modeler.pixelsToRatio(offset) * 2)));
    }

    /**
     * nastavi text komponenty a podle toho i jeji velikost
     *
     * @param text
     */
    public final void setText(String text) {
        this.text = text;
//        model.setText(text);
        model.setText(TextPiece.cut(text, textX, textY, font));
        recalculateMinSize();
    }

    /**
     * ocividne vraci text komponenty
     *
     * @return text komponenty
     */
    public String getText() {
        return text;
    }

    /**
     * zde je provadena akce po zmacknuti tlacitka
     */
    @Override
    protected void mousePressed() {
        for (ButtonListener listener : listeners) {
            listener.buttonClicked(new ButtonEvent(this, command, Mouse.getX(), Mouse.getY()));
        }

        if (model != null) {
            //zobrazit texturu clicked
            model.setTexture(clicked);
        }
    }

    @Override
    protected void recalculateMinSize() {

        int textHeightPx = font.getHeight(text);
        int textWidthPx = font.getWidth(text);

        int minWidthPx = textWidthPx + (2 * OUTLINE);
        int minHeightPx = textHeightPx + (2 * OUTLINE);//?? s tou sedmickou

        setMinSize(new ComponentPixelSize(minHeightPx, minWidthPx));

        if (parent != null) {
            parent.childChanged(this);
        }
    }

    @Override
    protected void mouseExited() {
        if (model != null) {
            //zobrazit texturu basic
            model.setTexture(basic);
        }
    }

    @Override
    protected void mouseEntered() {
        if (model != null) {
            //zobrazit texturu selected
            model.setTexture(selected);
        }
    }

    @Override
    protected void mouseReleased() {
        if (model != null) {
            //zobrazit texturu selected
            model.setTexture(selected);
        }
    }

    @Override
    protected void keyPressed(LWJGLKeyEvent e) {
    }

    @Override
    protected void mousePressedNotHere() {
    }
}
